#include <stdlib.h>
#include "cmd_core_utils.h"

/*去掉字符串开头和末尾的空白字符*/  
char *trimstr(char *string)
{  
    char *strp = NULL;
      
    trimrightblank(string);  
    strp = trimleftblank(string);  
      
    return strp;
}

/**
 *去掉字符串开头的空白字符，并返回新字符串指针
 */  
char * trimleftblank(char *string)  
{  
    char *strp = NULL;
      
    /*过滤空字符串*/  
    if(*string == '\0'){
        return string;
    }
    /*忽略头部的 空格 '/r' '/n' '/t'*/
    strp = string;
    while(*strp == ' ' || *strp == '\t' || *strp == '\r' || *strp == '\n'){
        strp++;
    }  
      
    return strp;  
}  
  
/*去掉字符串末尾的空白字符*/  
void trimrightblank(char *string)  
{  
    unsigned long strlenth = 0;  
      
    /*过滤空字符串*/
    if(*string == '\0'){
        return;  
    }  
      
    /*去掉尾部的 空格 '/r' '/n' '/t'*/  
    strlenth = strlen(string);      
    while(' ' == string[strlenth - 1] || '\t' == string[strlenth - 1]   
          || '\r' == string[strlenth - 1] || '\n' == string[strlenth - 1]){  
        strlenth--;  
    }  
    string[strlenth] = '\0';  
}

char *trim_left_delimiter(char *string, char delimiter)  
{  
    char *p_str = NULL;
      
    if(*string == '\0'){
        return string;
    }

    p_str = string;
    while(*p_str == delimiter){
        p_str++;
    }  
      
    return p_str;  
}  

int get_lenth_to_delimiter(const char *string, char delimiter)
{
	const char *p_str = string;

	if(*p_str == '\0'){
		return -1;
	}

	while((*p_str != delimiter)
			&&(*p_str != '\0')){
		p_str++;
	}

	return p_str - string;
}

char *jump_to_next_delimited_segment(char *string, char delimiter)
{
	char *cur_str = trim_left_delimiter(string, delimiter);

	int lenth_to_delimiter = get_lenth_to_delimiter(cur_str, delimiter);

	cur_str += lenth_to_delimiter;

	return trim_left_delimiter(cur_str, delimiter);
}

/*Will change the original string. Please backup the original string before 
 * call this function
 *Make sure ret_segment is large enough. You should get lenth to a delimiter 
 * by calling get_lenth_to_delimiter function first.
 */
static char *_get_delimited_string_segment(char *string, char delimiter, char *ret_segment)
{
	char *cur_str = string;
	char *cur_buf = ret_segment;

	if(*cur_str == '\0'){
		return NULL;
	}

	for(;*cur_str != delimiter && *cur_str != '\0';cur_str++,cur_buf++){
		*cur_buf = *cur_str;
	}

	*cur_buf = '\0';

	return ret_segment;
}

char *get_delimited_string_segment(char *string, char delimiter)
{
	char *cur_str = string;
	int lenth_to_delimiter;
	char *tmp_buf;

	cur_str = trim_left_delimiter(string, ' ');

	lenth_to_delimiter = get_lenth_to_delimiter(cur_str, delimiter);
	if(lenth_to_delimiter <= 0){
		return NULL;
	}

	tmp_buf = (char *)malloc(lenth_to_delimiter + 1);
	if(tmp_buf == NULL){
		return NULL;
	}

	return _get_delimited_string_segment(cur_str, delimiter, tmp_buf);
}

void realse_delimited_string_segment(char *segment)
{
	free(segment);
}

int parse_int_value(char *string)
{
	return atoi(string);
}

int8_t parse_on_off(char *string)
{
	int value;
	char *cur_str = string;

	char *cur_buf = get_delimited_string_segment(cur_str, ' ');

        if(strcmp(cur_buf,STR_ON) == 0){
		value = 1;
	}else if(strcmp(cur_buf,STR_OFF) == 0){
		value = 0;
	}else{
		value = -1;
        }

	realse_delimited_string_segment(cur_buf);
        return value;
}

int return_success_default(char *ret_buf, size_t ret_max_len)
{
	if(ret_buf == NULL){
		return 0;
	}

	strncpy(ret_buf,STR_SUCCESS,MIN(ret_max_len,strlen(STR_SUCCESS) + 1));
	return 0;
}

int return_fail_default(char *ret_buf, size_t ret_max_len)
{
	if(ret_buf == NULL){
		return -1;
	}

	strncpy(ret_buf, STR_FAIL, MIN(ret_max_len, strlen(STR_FAIL)));
	return -1;
}
